﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.UI;
using Hr.BLL;
using Hr.Model;
using HR.HrHelper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace HR.Areas.li_jialong.Controllers.Wage
{
    public class WageMangeController : ApiController
    {
        //导入BLL层
        protected WageService wage = new WageService();
        int code = 0;//0是成功，1是失败
        string msg = "success";//信息
        System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;

        /// <summary>
        /// 获取所有薪资信息集合API
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject GetAttendanceList(string SelTime)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",wage.GetAttendanceListAll(DateTime.Parse(SelTime)) }//调用bll层方法，获取数据
            };

            //返回
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 根据FullName查询Wage_List表数据
        /// </summary>
        /// <returns></returns>

        [HttpGet]
        public JObject FNameSelectWageList(string FullName)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",wage.FNameSelectWageList(FullName) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 添加、修改薪资信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JObject UpdateWageList()
        {

            // 调用jsonhelper帮助类转换成对象
            Wage_List r = JsonHelper.DeserializeJsonToObject<Wage_List>(RequestForm["WageInfo"]);
            string type = RequestForm["Type"];
            int pk_User = int.Parse(RequestForm["pk_User"].ToString());
            if (type.Equals("Edit"))
            {
                Wage_List SelRo = wage.GetWageInfo(r.pk_Wage);
                msg = "更新成功";
                if (SelRo == null)
                {
                    code = 1;
                    msg = "薪资信息不存在";
                }
                else
                {
                    if (ModelHelper.ReflectModel(r, SelRo))
                    {
                        msg = "无任何更新";
                    }
                    else
                    {
                        if (!wage.UpdateWageList(r, pk_User, type))
                        {
                            code = 1;
                            msg = "更新失败";
                        }
                    }
                }
            }
            else
            {
                Wage_List SelRo = wage.GetWageInfo(r.pk_Wage);
                msg = "添加成功";

                if (SelRo != null)
                {
                    code = 1;
                    msg = "角色已存在！";
                }
                else
                {
                    if (!wage.UpdateWageList(r,pk_User, type))
                    {
                        code = 1;
                        msg = "添加失败";
                    }
                }

            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 删除薪资信息，根据薪资ID
        /// </summary>
        /// <param name="pk_Name"></param>
        /// <returns></returns>
        [HttpPost]
        public JObject DelWagedata()
        {
            int pk_wage = int.Parse(RequestForm["pk_Wage"].ToString());
            Wage_List r = wage.GetWageInfo(pk_wage);
            msg = "删除成功";
            if (r != null)
            {
                if (!wage.DelWagedata(pk_wage))
                {
                    //失败
                    code = 1;
                    msg = "删除失败";
                }
            }
            else
            {
                code = 1;
                msg = "薪资不存在";
            }

            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 根据薪资ID查询单个角色或者根据薪资名称进行模糊查询多个角色
        /// </summary>
        [HttpGet]
        public JObject GetRoleName(string NameID, string Type, string NowTime)
        {
            object data = "";
            if (Type.Equals("SelName"))
            {
                //模糊查询
                data = NameID != null ? wage.SelWageList(NameID) : wage.GetAttendanceListAll(DateTime.Parse(NowTime));
            }
            else
            {
                int id = int.Parse(NameID);
                data = wage.GetWageInfo(id);
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",data}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 获取薪资当年的薪资相加数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject GetChartList()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",wage.GetChartInfo()}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 获取薪资去年的薪资相加数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject GetChart()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",wage.GetChart()}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
    }
}
